#include "head.h"
// 退票
void get_ticket (FlightSystem * Fly_list,U_data log_data,U_list * list ,D_list * order)
{
    printf("以下是您的订单信息\n");
    int L = findOrder(order, log_data.name);
    if (L == 0)
    {
        printf("您暂时还没有订单哟\n");
        sleep(2);
    }
    printf("请选择您要退票的订单\n");
    int id;
    scanf("%d", &id);
    D_node *ptr = R_List_find_id(order, id);
    F_node * fly = D_List_find_id(Fly_list->Flight_list, id);
    int K = R_List_find_id_n(order,id,log_data.name);
    R_List_delete(order, K);
    R_List_File_print(order, "./data/order_member.txt");
    log_data.numTickets--;
    log_data.value += ptr->data.ticket.value;
    fly->data.leftbuyersa++;
    D_List_file_print(Fly_list->Flight_list);
    U_node * ptr2;
    U_node * ptr3;
    U_node * ptr4;
    U_node * ptr5;
    switch (log_data.grade)
    {
    case 2:
        ptr2= S_List_find_name(Fly_list->REGULAR_USER , log_data.name);
        ptr2->data.value = log_data.value;
        ptr2->data.numTickets--;
        S_List_File_print(Fly_list->REGULAR_USER,REGULAR_FILE);
        break;
    case 3:
        ptr3= S_List_find_name(Fly_list->SILVER_USER , log_data.name);
        ptr3->data.value = log_data.value;
        ptr3->data.numTickets--;
        S_List_File_print(Fly_list->SILVER_USER,SILVER_FILE);
        break;
    case 4:
        ptr4= S_List_find_name(Fly_list->GOLD_USER , log_data.name);
        ptr4->data.value = log_data.value;
        ptr4->data.numTickets--;
        S_List_File_print(Fly_list->GOLD_USER,GOLD_FILE);
        break;
    case 5:
        ptr5= S_List_find_name(Fly_list->PLATINUM_USER , log_data.name);
        ptr5->data.value = log_data.value;
        ptr5->data.numTickets--;
        S_List_File_print(Fly_list->PLATINUM_USER,PLATINUM_FILE);
        break;
    default:
        break;
    }
    printf("退票成功\n");
    sleep(2);
}
// 改签
void ticket_changing (FlightSystem * Fly_list,U_data log_data,U_list * list ,D_list * order)
{
     printf("以下是您的订单信息\n");
    int L = findOrder(order, log_data.name);
    if (L == 0)
    {
        printf("您暂时还没有订单哟\n");
        sleep(2);
    }
    printf("请选择您要改签的订单\n");
    int id;
    scanf("%d", &id);
    D_List_print(Fly_list->Flight_list);
    sleep(1);
    printf("请选择您的新航班\n");
    int new_id;
    scanf("%d", &new_id);
    F_node * new_fly = D_List_find_id(Fly_list->Flight_list, new_id);
    D_node *ptr = R_List_find_id(order, id);
    F_node * fly = D_List_find_id(Fly_list->Flight_list, id);
    if (new_fly->data.value > log_data.value)
    {
        printf("抱歉,您的余额暂不支持改签新航班\n");
        sleep(2);
        return ;
    }
    
    if (ptr->data.ticket.value > new_fly->data.value)
    {
        log_data.value += (ptr->data.ticket.value - new_fly->data.value);
    }
    if (ptr->data.ticket.value < new_fly->data.value)
    {
        log_data.value -= (new_fly->data.value - ptr->data.ticket.value);
    }
    ptr->data.ticket = new_fly->data;
    R_List_File_print(order, "./data/order_member.txt");
    fly->data.leftbuyersa++;
    new_fly->data.leftbuyersa--;
    D_List_file_print(Fly_list->Flight_list);
    U_node * ptr2;
    U_node * ptr3;
    U_node * ptr4;
    U_node * ptr5;
    switch (log_data.grade)
    {
    case 2:
        ptr2= S_List_find_name(Fly_list->REGULAR_USER , log_data.name);
        ptr2->data.value = log_data.value;
        S_List_File_print(Fly_list->REGULAR_USER,REGULAR_FILE);
        break;
    case 3:
        ptr3= S_List_find_name(Fly_list->SILVER_USER , log_data.name);
        ptr3->data.value = log_data.value;
        S_List_File_print(Fly_list->SILVER_USER,SILVER_FILE);
        break;
    case 4:
        ptr4= S_List_find_name(Fly_list->GOLD_USER , log_data.name);
        ptr4->data.value = log_data.value;
        S_List_File_print(Fly_list->GOLD_USER,GOLD_FILE);
        break;
    case 5:
        ptr5= S_List_find_name(Fly_list->PLATINUM_USER , log_data.name);
        ptr5->data.value = log_data.value;
        S_List_File_print(Fly_list->PLATINUM_USER,PLATINUM_FILE);
        break;
    default:
        break;
    }
    printf("改签成功\n");
    sleep(2);
}